//
//  YWLiveController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/29.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWLiveController.h"
#import "ZYLiveDataModels.h"
#import "YWHttpTool.h"
#import "YWLiveCell.h"
#import "YWLiveOtherCell.h"
#import "YWLiveHeaderView.h"
#import "YWLiveSmallCell.h"
#import "ZYLiveSpecialextra.h"

@interface YWLiveController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSArray *itemArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *sectionArr;
@end

@implementation YWLiveController

- (void)viewDidLoad {

    [self requestData];

}


-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [YWHttpTool GET:@"http://c.m.163.com/nc/live/list/all/0-20.html" params:nil success:^(NSDictionary *responseObject) {
        ZYLiveNSObject *obj = [ZYLiveNSObject modelObjectWithDictionary:responseObject];
        _itemArray = obj.t1462958418713;
        [weakSelf.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

-(NSMutableArray *)sectionArr
{
    if (!_sectionArr)
    {
        _sectionArr = [NSMutableArray array];
    }
    return _sectionArr;
}
-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView            = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT)];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"YWLiveCell" bundle:nil] forCellReuseIdentifier:@"YWLiveCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"YWLiveOtherCell" bundle:nil] forCellReuseIdentifier:@"YWLiveOtherCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"YWLiveSmallCell" bundle:nil] forCellReuseIdentifier:@"YWLiveSmallCell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    self.sectionArr = nil;
    for (int i =0; i<_itemArray.count; i++)
    {
        ZYLiveT1462958418713 *model = _itemArray[i];
        if (model.specialextra.count>0)
        {
            [self.sectionArr addObject:model];
        }
    }
    return self.sectionArr.count+1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == self.sectionArr.count)
    {
        return 1;
    }
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYLiveT1462958418713 *model = _itemArray[indexPath.row];
        if (indexPath.section ==0)
        {
            if (indexPath.row == 0)
            {
                YWLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YWLiveCell"];
                cell.model = model;
                return cell;
            }else
            {
                YWLiveOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YWLiveOtherCell"];
                cell.model = model;
                return cell;
            }
            
        }else if(indexPath.section < self.sectionArr.count){
            
                YWLiveSmallCell *cell          = [tableView dequeueReusableCellWithIdentifier:@"YWLiveSmallCell"];
                ZYLiveT1462958418713 *secModel = self.sectionArr[indexPath.section];
                NSArray *secArray1             = secModel.specialextra;
                ZYLiveSpecialextra *mod        = secArray1[indexPath.row];
                cell.model                     = mod;
                return cell;
        }

    YWLiveOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YWLiveOtherCell"];
    ZYLiveT1462958418713 *lastModel = [_itemArray lastObject];
                cell.model = lastModel;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == self.sectionArr.count)
    {
        return nil;
    }
    ZYLiveT1462958418713 *model = self.sectionArr[section];
    YWLiveHeaderView *headerView = [YWLiveHeaderView new];
    headerView.model = model;
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section==0||section == self.sectionArr.count?0:[YWLiveHeaderView fixHeaderViewHeight];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row != 0 )
        {
            return 180;
        }
        return 250;
    }else if (indexPath.section == self.sectionArr.count)
    {
        return 180;
    }
    return 78;
}
@end
